﻿namespace CKFinder.Settings
{
    using System;
    using System.Web;
    using System.Web.UI;

    public class ResourceType
    {
        private string _Url;
        public string[] AllowedExtensions;
        public string[] DeniedExtensions;
        public string Dir;
        public int MaxSize;
        internal string Name;

        internal ResourceType(string name)
        {
            this.Name = name;
            this.Url = "";
            this.Dir = "";
            this.MaxSize = 0;
            this.AllowedExtensions = new string[0];
            this.DeniedExtensions = new string[0];
        }

        public bool CheckExtension(string extension)
        {
            extension = extension.TrimStart(new char[] { '.' }).ToLower();
            if ((this.DeniedExtensions.Length > 0) && (Array.IndexOf<string>(this.DeniedExtensions, extension) >= 0))
            {
                return false;
            }
            if (this.AllowedExtensions.Length > 0)
            {
                return (Array.IndexOf<string>(this.AllowedExtensions, extension) >= 0);
            }
            return true;
        }

        public string GetTargetDirectory()
        {
            if (this.Dir.Length == 0)
            {
                return HttpContext.Current.Server.MapPath(this.Url);
            }
            return this.Dir;
        }

        public string Url
        {
            get
            {
                return this._Url;
            }
            set
            {
                this._Url = value;
                if (this._Url.StartsWith("~"))
                {
                    this._Url = ((Page) HttpContext.Current.Handler).ResolveUrl(this._Url);
                }
                if (!this._Url.EndsWith("/"))
                {
                    this._Url = this._Url + "/";
                }
            }
        }
    }
}

